package com.r2coding.server.modules.user.controller;

import com.r2coding.core.response.Result;
import com.r2coding.core.utils.IdUtil;
import com.r2coding.server.common.annotation.LoginIgnore;
import com.r2coding.server.common.utils.UserIdUtil;
import com.r2coding.server.modules.user.po.*;
import com.r2coding.server.modules.user.service.IUserService;
import com.r2coding.server.modules.user.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 98k灬
 * @version v1.0.0
 * @Description : 用户控制器
 * @Create on : 2023/9/24 09:46
 **/
@Validated
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @LoginIgnore
    @PostMapping("/register")
    public Result<?> register(@Validated @RequestBody UserRegisterPo userRegisterPo) {
        Long userId = userService.register(userRegisterPo);
        return Result.ok(IdUtil.encrypt(userId));
    }

    @LoginIgnore
    @PostMapping("/login")
    public Result<?> login(@Validated @RequestBody UserLoginPo userLoginPo) {
        String token = userService.login(userLoginPo);
        return Result.ok(token);
    }

    @PostMapping("/exit")
    public Result<?> exit() {
        userService.exit(UserIdUtil.get());
        return Result.ok();
    }

    @LoginIgnore
    @PostMapping("/username/check")
    public Result<?> checkUsername(@Validated @RequestBody CheckUserNamePo checkUserNamePo) {
        String question = userService.checkUsername(checkUserNamePo);
        return Result.ok(question);
    }

    @LoginIgnore
    @PostMapping("/answer/check")
    public Result<?> checkAnswer(@Validated @RequestBody CheckAnswerPo checkAnswerPo) {
        String token = userService.checkAnswer(checkAnswerPo);
        return Result.ok(token);
    }

    @LoginIgnore
    @PostMapping("/password/reset")
    public Result<?> passwordReset(@Validated @RequestBody ResetPasswordPo resetPasswordPo) {
        userService.passwordReset(resetPasswordPo);
        return Result.ok();
    }

    @PostMapping("/password/change")
    public Result<?> changePwd(@Validated @RequestBody ChangePasswordPo changePasswordPo) {
        userService.changePwd(changePasswordPo);
        return Result.ok();
    }

    @GetMapping
    public Result<?> info() {
        UserInfoVo userInfoVo = userService.info();
        return Result.ok(userInfoVo);
    }
}
